package com.vichat.common.util;

import com.alibaba.fastjson.JSONArray;

import java.util.*;

/**
 * Created by wangluyin on 16/9/27.
 */
public class NumberUtils extends org.apache.commons.lang3.math.NumberUtils {
    public static boolean isNullOrZero(Long value) {
        if (value == null || value == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNullOrZero(Integer value) {
        if (value == null || value == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNull(Long value) {
        if (value == null) {
            return true;
        }
        return false;
    }

    public static boolean isNull(Integer value) {
        if (value == null) {
            return true;
        }
        return false;
    }

    public static boolean isNotNullOrZero(Long value) {
        return !isNullOrZero(value);
    }

    public static boolean isNotNullOrZero(Integer value) {
        return !isNullOrZero(value);
    }

    public static boolean isNotNull(Long value) {
        return !isNull(value);
    }

    public static boolean isNotNull(Integer value) {
        return !isNull(value);
    }

    /**
     * 升序排列
     *
     * @param value
     * @return
     */
    public static String sortAsc(long[] value) {
        String key = "";
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < value.length; i++) {
            list.add(value[i]);
        }
        Collections.sort(list);
        for (long val : list) {
            key += val + ",";
        }
        return key.substring(0, key.length() - 1);
    }

    public static long[] toArray(JSONArray values) {
        if (values == null || values.size() == 0) {
            return new long[]{};
        }
        long[] destArray = new long[values.size()];
        for (int i = 0; i < values.size(); i++) {
            destArray[i] = values.getLongValue(i);
        }
        return destArray;
    }

    public static int unAbs(int a) {
        return (a > 0) ? -a : a;
    }

    public static String getNumSmallCharRandom(int length) {
        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出小写字母
                val += (char) (random.nextInt(26) + 97);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                //输出数字
                val += String.valueOf(random.nextInt(10));
            }
        }

        return val;
    }
}
